<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include_once 'base_controller.php';

class Goods extends Base_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('goods_model');
    }
    
    public function search()
    {
        if(isset($_POST['value']) && $_POST['value'] != '')
        {
            $array = array('select'=>$_POST['select'], 'selectValue'=>$_POST['value']);
            $this->session->set_userdata($array);
        }
        
        if(isset($_POST['value']) && $_POST['value'] == '')
        {
            redirect('goods/index');
        }
        
        //var_dump(self::$select);exit;
        $perPage = 20;
        $offset = (int)$this->uri->segment(3, 0);
        $this->load->library('pagination');

        $config['base_url'] = site_url('goods/search');
        $config['total_rows'] = $this->goods_model->getTotalCount($this->session->userdata('select'), $this->session->userdata('selectValue'));
        $config['per_page'] = $perPage; 
        $config['uri_segment'] = 3;
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<li class="">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li class="">';
        $config['prev_tag_close'] = '</li>';
        
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        
        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li class="">';
        $config['last_tag_close'] = '</li>';
        
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $config['cur_tag_open'] = '<li class="page"><a>';
        $config['cur_tag_close'] = '</a></li>';

        $this->pagination->initialize($config); 

        //获取分页html字符串
        $pager = $this->pagination->create_links();

        $goods = $this->goods_model->getList($perPage, $offset, $this->session->userdata('select'), $this->session->userdata('selectValue'));
        $data = array(
                'goods'=>$goods,
                'pager'=>$pager,
        );
        
        $this->load->view('goodslist', $data);
        $this->load->view('footer');
    }


    public function index()
    {
        $perPage = 20;
        $offset = (int)$this->uri->segment(3, 0);
        $this->load->library('pagination');

        $config['base_url'] = site_url('goods/index');
        $config['total_rows'] = $this->goods_model->getTotalCount();
        $config['per_page'] = $perPage; 
        $config['uri_segment'] = 3;
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<li class="">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li class="">';
        $config['prev_tag_close'] = '</li>';
        
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        
        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li class="">';
        $config['last_tag_close'] = '</li>';
        
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $config['cur_tag_open'] = '<li class="page"><a>';
        $config['cur_tag_close'] = '</a></li>';

        $this->pagination->initialize($config); 

        //获取分页html字符串
        $pager = $this->pagination->create_links();

        $goods = $this->goods_model->getList($perPage, $offset);
        $data = array(
                'goods'=>$goods,
                'pager'=>$pager,
        );
        
        $this->load->view('goodslist', $data);
        $this->load->view('footer');
    }
    
    public function specStock()
    {
        $goodsid = (int)$this->uri->segment(3, 0);
        $array = $this->goods_model->getSpecStockByGoodsId($goodsid);
        
        if($array)
        {
            echo json_encode($array);
            exit;
        }
        else
        {
            echo json_encode("错误！");
            exit;
        }
    }
    
    public function modify()
    {
        $goodsid = (int)$this->uri->segment(3, 0);
        //var_dump($_POST);exit;
        //var_dump($this->uri->uri_string());exit;
        if( $this->goods_model->modifySpecStockByGoodsId($goodsid))
        {
            redirect($_POST['cururl']);
        }
        else
        {
            echo "出现错误！，请返回重新操作！";
            exit;
        }
    }
}